import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { Shortcuts } from "../view/shortcuts/Shortcuts";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { SPetUpdate } from "../../../Message/Game/SPetUpdate";
import { SystemSetter } from "../view/system/SystemSetter";
import { CDBackPackItem } from "../cache/CDItem/CDBackPackItem";
import { SkillRules } from "../rules/SkillRules";
import { ECategory } from "../../../Message/Public/ECategory";
import { EDrug } from "../../../Message/Public/EDrug";
import { SkillInfo } from "../model/SkillInfo";
import { GameController } from "../mvc/GameController";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { ScreenOperation } from "../scene/ScreenOperation";
import { Cache } from "../cache/Cache";
import { SkillConst } from "../view/skill/SkillConst";
import { CDItemData } from "../cache/CDItem/CDItemData";
import { ItemData } from "../resource/info/ItemData";
import { GameProxy } from "../mvc/GameProxy";
type int = number;
//class ShortcutsController
    
    export  class ShortcutsController extends Controller
    {
        constructor()
        {
            
            super();this.initView();
            return;
        }

        protected /* override */ initView(): IView
        {
            this._shortcuts = new Shortcuts();
            return this._shortcuts;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.ShortCutsKey, this.onUseShortCutHandler);
            NetDispatcher.addCmdListener(ServerCommand.SkillListRes, this.onSkillListRes);
            NetDispatcher.addCmdListener(ServerCommand.SkillUpdate, this.updateShortCutSkillId);
            NetDispatcher.addCmdListener(ServerCommand.SkillMarryRmoved, this.removeShortCutMarrySkill);
            Dispatcher.addEventListener(EventName.AddToBuffSkillByReq, this.onAddToBuffSkillByReqHandler);
            Dispatcher.addEventListener(EventName.RemoveBuffSkillByReq, this.removeBuffSkillByReqHandler);
            Dispatcher.addEventListener(EventName.UpdateBuffSkillCDReq, this.UpdateBuffSkillCDReqHandler);
            Dispatcher.addEventListener(EventName.Skill_CoolDown, this.useSkillItem);
            Dispatcher.addEventListener(EventName.Skill_CoolDown_Clear, this.onSkillCoolDownClearHandler);
            Dispatcher.addEventListener(EventName.ItemMomentDrug_CoolDown_Clear, this.onItemMomentDrugCoolDownClearHandler);
            Dispatcher.addEventListener(EventName.AddToShortcutsByReq, this.addToShortcutsByReq);
            Dispatcher.addEventListener(EventName.SaveShortcutsData, this.onSaveShortcutsDataHandler);
            Dispatcher.addEventListener(EventName.Fight_Process_Begin, this.fightProcessBegin);
            NetDispatcher.addCmdListener(ServerCommand.ShortCutsMsg, this.updateShortCuts);
            Dispatcher.addEventListener(EventName.ShortCutsItemChange, this.shortCutItemChange);
            Dispatcher.addEventListener(EventName.ItemUse, this.onUseItemHandler);
            Dispatcher.addEventListener(EventName.SysSettingSuccess, this.onSysSettingSuccessHandler);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            return;
        }

        /* internal  */onPetBagUpdateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPetUpdate;
            this._shortcuts.updatePetInfo(loc1);
            return;
        }

        /* internal  */onSysSettingSuccessHandler(arg1: DataEvent): void
        {
            this._shortcuts.isLock = SystemSetter.currentSetter.isLockShortcut;
            return;
        }

        /* internal  */onUseItemHandler(arg1: DataEvent): void
        {
            this._shortcuts.useItem(arg1.data as CDBackPackItem);
            return;
        }

        /* internal  */useSkillItem(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            var loc2=cache.skill.getSkillInfoById(loc1);
            var loc3=false;
            if (loc2) 
            {
                if (SkillRules.isMarrySkill(loc2.tSkill)) 
                {
                    this._shortcuts.useSkillItem(loc2, true, false);
                }
                else 
                {
                    if (loc2.clearOtherCd) 
                    {
                        cache.skill.clearAllSkillCD();
                    }
                    loc3 = loc2.leadTime > 0 || loc2.useTime > 0 ? false : true;
                    this._shortcuts.useSkillItem(loc2, true, loc3);
                }
            }
            return;
        }

        /* internal  */onSkillCoolDownClearHandler(arg1: DataEvent): void
        {
            cache.skill.clearAllSkillCD();
            return;
        }

        /* internal  */onItemMomentDrugCoolDownClearHandler(arg1: DataEvent): void
        {
            cache.timeCdCache.clearItemCdTime(ECategory._ECategoryDrug, EDrug._EDrugLife, 1);
            cache.timeCdCache.clearItemCdTime(ECategory._ECategoryDrug, EDrug._EDrugMana, 1);
            return;
        }

        /* internal  */fightProcessBegin(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            var loc2=cache.skill.getSkillInfoById(loc1);
            if (loc2) 
            {
                this._shortcuts.useSkillItem(loc2, false);
            }
            return;
        }

        public onUseShortCutHandler(arg1: DataEvent): void
        {
            var event: DataEvent;
            var skill: SkillInfo;

            var loc1;
            skill = null;
            event = arg1;
            if (GameController.stall.isInStall) 
            {
                MsgManager.showRollTipsMsg(Language.getString(51084));
                return;
            }
            if (cache.skill.skillSpecialMaps.length > 0 && GameMapUtil.curMapState.isBossField) 
            {
                return;
            }
            if (cache.copy.isHeroBattleCopy()) 
            {
                return;
            }
            if (cache.skill.skillSpecialMaps.length > 0 && GameMapUtil.curMapState.isRobFlag) 
            {
                return;
            }
            if (cache.skill.skillSpecialMaps.length > 0 && GameMapUtil.curMapState.isCrossDefenceCopy) 
            {
                return;
            }
            if (cache.role.isBoss()) 
            {
                return;
            }
            if (RolePlayer.instance.isDoubleRest) 
            {
                ScreenOperation.interruptDoubleRest((): void
                {
                    _shortcuts.useShortCut(event.data as int);
                    return;
                })
                return;
            }
            if (Cache.instance.fish.isInFishing) 
            {
                skill = this._shortcuts.getSkillByKey(event.data as int);
                if (skill && skill.skillId == SkillConst.Fish) 
                {
                    return;
                }
                ScreenOperation.interruptFish((): void
                {
                    _shortcuts.useShortCut(event.data as int);
                    return;
                })
                return;
            }
            this._shortcuts.useShortCut(event.data as int);
            return;
        }

        public onAddToBuffSkillByReqHandler(arg1: DataEvent): void
        {
            this._shortcuts.updateNavSkill(arg1.data as SkillInfo);
            return;
        }

        public removeBuffSkillByReqHandler(arg1: DataEvent): void
        {
            this._shortcuts.removeNavSkill(arg1.data as SkillInfo);
            return;
        }

        /* internal  */UpdateBuffSkillCDReqHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["cdItemData"] as CDItemData;
            var loc2=arg1.data["skillId"];
            this._shortcuts.updateNavSkillCd(loc1, loc2);
            return;
        }

        public onSkillListRes(arg1: Array<any>): void
        {
            this._shortcuts.onSkillListRes(cache.skill.skillMaps.concat(cache.skill.skillBase));
            return;
        }

        /* internal  */updateShortCutSkillId(arg1: SkillInfo): void
        {
            this._shortcuts.updateSkill(arg1);
            return;
        }

        /* internal  */removeShortCutMarrySkill(arg1: Object): void
        {
            this._shortcuts.removeMarrySkill();
            return;
        }

        /* internal  */updateShortCutItemId(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */updateShortCutItemIdByParentThrow(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */updataShortcutsSameDrugItemUseOver(arg1: DataEvent): void
        {
            this._shortcuts.updataShortcutsSameItem(arg1.data as ItemData);
            return;
        }

        /* internal  */addToShortcutsByReq(arg1: DataEvent): void
        {
            this._shortcuts.addItemToShortcuts(arg1.data);
            return;
        }

        /* internal  */onSaveShortcutsDataHandler(arg1: DataEvent): void
        {
            GameProxy.shortcutsProxy.saveShortCust(arg1.data as string);
            return;
        }

        /* internal  */updateShortCuts(arg1): void
        {
            this._shortcuts.updateShortCuts(arg1, cache.role.roleInfo.careerEnable);
            return;
        }

        public shortCutItemChange(arg1: DataEvent): void
        {
            this._shortcuts.updateItemNumber();
            return;
        }

        public getSkillPos(arg1: int): Object /* flash.geom.Point */
        {
            return this._shortcuts.getScItemPos(arg1);
        }

        private /* var */_shortcuts: Shortcuts;
    }
